<?php
//全景图片
if(!defined('IN_T')){
   die('hacking attempt');
}
$act = Common::sfilter($_REQUEST['act']);

if($act=='delete'){
    $ret['status'] = 0;
	$id = intval($_REQUEST['id']);
	$Db->delete($Base->table('topic'),array('id'=>$id));
	$ret['status'] = 1;
	echo  $Json->encode($ret);
	exit;
}
else if($act=="search"){
	$pname = Common::sfilter($_REQUEST['pname']);
	$res = get_project($pname);
	echo $Json->encode($res);
	exit;
}
//添加、编辑专题
else if($act == "detail"){
	$id = intval($_REQUEST['id']);
	//提取专题的全景项目
	$p = $Db->query("select * from ".$Base->table('topic')." where id=$id",'Row');
	//传入的$id不存在，强制赋值为0（添加）
	if(!$p){
		$id = 0;
	}
	//显示界面
	if(empty($_POST)){
		$tp->assign('p',$p);
		$tp->assign('worksmain',get_project());
		$tp->assign('topic_cats',get_topic_cats());
	}
	//执行操作
	else{
		$data = array(
			'tid'=>intval($_POST['tid']),
			'pid'=>intval($_POST['pid']),
			'pname'=>Common::sfilter($_POST['pname']),
			'sort_order'=>intval($_POST['sort_order']),
		);
		$re['status'] = 0;
		//判断tid是否存在
		if(!$t=$Db->query("select id from ".$Base->table('topic_cat')." where id=".$data['tid']."",'Row')){
			$re['msg'] = '请选择一个专题';
		}
		//判断pid是否存在
		else if(!$w=$Db->query("select thumb_path from ".$Base->table('worksmain')." where pk_works_main=".$data['pid']."",'Row')){
			$re['msg'] = '请选择一个全景项目';
		}
		//判断pname是否为空
		else if(empty($data['pname'])){
			$re['msg'] = '请填写全景名称';
		}
		else{
			$data['img_path'] = $w['thumb_path'];
			//$id存在则是修改
			if($id){
				$Db->update($Base->table('topic'),$data,array('id'=>$id));
				$re['msg'] = '编辑成功';
			}
			//添加
			else{
				$Db->insert($Base->table('topic'),$data);
				$re['msg'] = '添加成功';
			}
			$re['status'] = 1;
			$re['href'] = '/'.$_lang['admin_path'].'/?m=topic';
		}
		echo  $Json->encode($re);
		exit;
	}
}
else{
    $size = 15; //定义每页显示10条
	$page = intval($_REQUEST['page']);
	$page = $page<1 ? 1 : $page;
	$tid = intval($_REQUEST['tid']);
	$res = get_topic_list($page,$size,$tid);
	$pages = Common::set_page($size,$page,$res['count']);
	foreach ($pages as $key => $value) {
		$pages[$key]['url'] = "/".ADMIN_PATH."/?m=topic&page=".$value['num'].($tid>0?"&tid=$tid":'');
	}
	
	$tp->assign("page",$page);
	$tp->assign("pages",$pages);
	$tp->assign("res",$res);
	$tp->assign('topic_cats',get_topic_cats());
}
$tp->assign('nav','专题管理');
$tp->assign('act',$act);

//查询所有专题
function get_topic_cats(){
	$sql = "select * from ".$GLOBALS['Base']->table('topic_cat')." order by id desc";
	$res = $GLOBALS['Db']->query($sql);
	return $res;
}

//查询专题的项目
function get_topic_list($page=0,$size=10,$tid){
	$sql = "select t.*, tc.name as tname, w.view_uuid from ".$GLOBALS['Base']->table('topic')." as t ".
		   "left join ".$GLOBALS['Base']->table('topic_cat')." as tc on tc.id=t.tid ".
		   "left join ".$GLOBALS['Base']->table('worksmain')." as w on w.pk_works_main=t.pid ".
		   "where 1 ";
	if($tid>0){
		$sql .= "and t.tid=$tid ";
		$GLOBALS['tp']->assign('tid',$tid);
	}
	$res['count'] = count($GLOBALS['Db']->query($sql));
	
	$sql .= "order by t.sort_order asc, t.id desc limit ".($page-1)*$size.", ".$size."";
	//echo $sql;
	$res['res'] = $GLOBALS['Db']->query($sql,"All");
	//print_r($res['res']);
	return $res;
}

//提取全景项目
function get_project($pname){
	$sql = "SELECT pk_works_main,name,view_uuid FROM ".$GLOBALS['Base']->table('worksmain')." WHERE 1 ";
	if($pname){
		$sql .=" AND name LIKE '%$pname%'";
	}
	$sql .= "order by pk_works_main desc";
	$res = $GLOBALS['Db']->query($sql);
	//print_r($res);
	return $res ? $res : array();
}
?>